package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonUtils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantVodUtils;
import com.atguigu.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
//@CrossOrigin //解决跨域问题
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     * 上传视频到 阿里云
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadALiYunVideo")
    public R uploadALiYunVideo(MultipartFile file) {
        //返回上传视频的ID值
        String videoId = vodService.uploadAliYunVideo(file);
        return R.ok().data("videoId", videoId);
    }

    /**
     * 根据视频ID删除 阿里云的课程视频
     *
     * @param videoId
     * @return
     */
    @DeleteMapping("/removeALiYunVideoById/{videoId}")
    public R removeALiYunVideoById(@PathVariable String videoId) {

        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建一个删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request里面设置视频ID
            request.setVideoIds(videoId);
            //调用初始化对象的方法 实现删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }
    }


    /**
     * 删除多个阿里云视频的方法 传递多个视频ID参数
     *
     * @param videoIdList
     * @return
     */
    @DeleteMapping("deleteBatchVideo")
    public R deleteBatchVideo(@RequestParam("videoIdList") List<String> videoIdList) {

        vodService.removeMoreALiYunVideo(videoIdList);
        return R.ok();
    }

    /**
     * 根据视频ID获取视频播放凭证
     *
     * @return
     */
    @GetMapping("getPlayAuth/{videoId}")
    public R getPlayAuth(@PathVariable String videoId) {

        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);

            //创建获取凭证的 request 和 response 对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();

            //向request里面设置视频的ID
            request.setVideoId(videoId);

            //调用方法得到视频的凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();

            return R.ok().data("playAuth", playAuth);
        } catch (Exception e) {
            throw new GuliException(20001, "获取视频凭证失败");
        }
    }


}
